/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.animation
{
	import flash.display.FrameLabel;
	import flash.display.MovieClip;

	public class AnimatedMovieClip
	{
		public var Id:String;
		public var Labels:Vector.<FrameLabel>;
		private var movieClip:MovieClip;
		
		public function AnimatedMovieClip(Id:String, Labels:Vector.<FrameLabel>, movieClip:MovieClip)
		{
			super();
			this.movieClip = movieClip;
			this.Id = Id;
			this.Labels = Labels;
			
			movieClip.gotoAndStop(1);
		}
		
		public function play(event:AnimatedEvent = null):void{
			movieClip.play();
		}
		
		public function stop():void{
			movieClip.stop();
		}
		
		public function gotoAndStop(frame:FrameLabel):void{
			movieClip.gotoAndStop(frame.frame);
		}
		
		public function nextFrame():void{
			movieClip.nextFrame();
		}
		
		public function prevFrame():void{
			movieClip.prevFrame();
		}
		
		public function prevtFrameLabel(Label:FrameLabel):Boolean{
			if(movieClip.currentFrame > 1){
				movieClip.prevFrame();
				if(movieClip.currentLabel != Label.name){
					movieClip.nextFrame();
					return false;
				}
				return true;
			}else{
				return false;
			}
		}
		
		
		public function nextFrameLabel(Label:FrameLabel):Boolean{
			if(movieClip.currentFrame < movieClip.totalFrames){
				movieClip.nextFrame();
				if(movieClip.currentLabel != Label.name){
					movieClip.prevFrame();
					return false;
				}
				return true;
			}else{
				return false;
			}
		}
		
		public function testMovieClipLabels():Boolean{
			var result:Boolean = true;
			for each(var label:FrameLabel in movieClip.currentLabels){
				if(!labelExist(label)){
					result = false;
					break;
				}
			}
			
			return result;
		}
		
		public function labelExist(TestLabel:FrameLabel):Boolean{
			for each(var label:FrameLabel in Labels){
				if(TestLabel.name == label.name){
					return true;
				}
			}
			
			return false;			
		}
	}
}